/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package client;

/**
 *
 * @author Welcome
 */
import java.awt.*;
import java.net.*;
import java.applet.*;

class  game extends Frame implements Runnable
{
	private clientwindow cw;
	private Thread thread;
	private String nextMove = "test";
	private String opponent_move;
	private String move;
	private boolean you_go_first;
	private boolean move_made = false;
	private boolean sent_my_move = false;
	private URL base_url;
	private TextField opponentfield;
    	private TextField textField;
	private Label userlabel;
	private Label userlabel2;
	private Label userlabel3;
	private int framewidth = 400;
	private int frameheight = 300;
	Applet myapplet;
  	graphicpanel gp;
	AudioClip mysound;
	Image Gameimage;

// constructor
game (Applet myapplet, URL base_url, clientwindow parent, boolean you_go_first, Image gameimage)
{
	super("Test Game Window");
	this.base_url = base_url;
	this.cw = parent;
	this.you_go_first = you_go_first;
	this.Gameimage = gameimage;
     	mysound = myapplet.getAudioClip(base_url, "intelmmx.wav");
	this.start();

}

// gets the thread going
public void start()
{
	System.out.println("starting thread");
  	thread = new Thread(this,"Game");
  	thread.setPriority(Thread.MIN_PRIORITY);
 	thread.start();
}

public void run()
{
	mysound.play();
 	System.out.println("RUNNING thread");
	if (!you_go_first)
	{
		sent_my_move = true;
	}

	makewindow();
      	while (true)
	{
		try{thread.sleep(20);}
		catch(InterruptedException e){}

		// wait for opponent's move
		if (sent_my_move)
		{
			move = this.get_opponent_move();
			opponentfield.setText(move);

			// process the move
		}

 	} // loop until the game is over
}


// called by clientwindow Don't change this!!!
public synchronized void set_opponent_move(String nextMove)
{
	move_made = true;
	System.out.println("setting opponent's next move is " + nextMove);
	opponent_move = nextMove;
	notifyAll();
}

// Don't change this!!!
public synchronized String get_opponent_move()
{
	if (!move_made)
	{
		try
		{
			wait();
		}
		catch (Exception e)
		{
			System.out.println("Exception in wait " + e.toString());
		}
	}
	System.out.println("get opponent's next move " + opponent_move);
	move_made = false;
	sent_my_move = false;
	return (opponent_move);

}

public void send_move (String nextMove)
{

	if (!sent_my_move)
	{
		System.out.println("waiting for send my move its false\n");
	}
	cw.sendMove(nextMove); // sends move to other player
	sent_my_move = true;
	System.out.println("sent move " + nextMove);

}

// You re-write all this...
public void makewindow()
{
    		this.setBackground(Color.black);
		this.setForeground(Color.black);
	     	this.setLayout(new FlowLayout());

		gp = new graphicpanel(Gameimage);
		gp.setSize(300,100);
		this.add(gp);


		userlabel3= new Label();
		if (you_go_first)
			userlabel3.setText("Player 1: Type in redbox & hit enter to send move to Opponent");
		else
			userlabel3.setText("Player 2: Type in redbox & hit enter to send move to Opponent");

    		userlabel3.setFont(new Font("Helvetica",Font.PLAIN,16));
		userlabel3.setForeground(Color.white);
		userlabel3.setBackground(Color.black);
		this.add(userlabel3);

		textField=new TextField(41);
    		textField.setFont(new Font("Helvetica",Font.PLAIN,16));
		textField.setBackground(Color.red);
		textField.setForeground(Color.white);
		this.add(textField);

		userlabel2= new Label();
		userlabel2.setText("This is Opponent's move via socket");
    		userlabel2.setFont(new Font("Helvetica",Font.PLAIN,16));
		userlabel2.setForeground(Color.white);
		userlabel2.setBackground(Color.black);
		this.add(userlabel2);

		opponentfield = new TextField(41);
    		opponentfield.setFont(new Font("Helvetica",Font.PLAIN,14));
		opponentfield.setBackground(Color.white);
		opponentfield.setForeground(Color.black);
		opponentfield.setEditable(false);
    		this.add(opponentfield);

 		this.add(new Button("QUIT"));

        	this.setSize(framewidth, frameheight);
 		this.show();
}

public boolean action(Event evt, Object arg)
{
    	if (evt.target instanceof Button)
	{
         	String label = (String)arg;
       	if (label.equals("QUIT"))
		{
			cw.quitWindow();
	        	this.dispose();
                	return true;
		}

    	}
    	if (evt.target == textField)
	{
		if (!sent_my_move)
		{
			nextMove = textField.getText();
           		send_move(nextMove);
			if (you_go_first)
				userlabel3.setText("Player 1: Type in redbox & hit enter to send move to Opponent");
			else
				userlabel3.setText("Player 2: Type in redbox & hit enter to send move to Opponent");
			return true;
		}
		else
			userlabel3.setText("You can not move twice, wait for your opponet's move");
	}
    return false;
}

} // end Game class
